package com.project.smarthome.login.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.project.smarthome.R;
import com.project.smarthome.common.util.UIUtils;
import com.project.smarthome.common.view.ImageLoader;
import com.project.smarthome.login.model.NameAndPhone;

import java.io.File;
import java.util.List;


/**
 * 作者 lunanting on 2016/6/27 15 20.
 * <p/>
 * 邮箱：919530038@qq.com
 */
public class UserHisAdapter extends RecyclerView.Adapter<UserHisAdapter.ViewHolder> {

    private Context mContext;
    private List<NameAndPhone> mList;
    private ItemClickListener mItemClickListener;

    public List<NameAndPhone> getList() {
        return mList;
    }

    public void setList(List<NameAndPhone> list) {
        mList = list;
    }

    public UserHisAdapter(Context context) {
        mContext = context;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public NameAndPhone getItem(int pos) {
        if (mList == null && mList.isEmpty())
            return null;
        return mList.get(pos);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_recyitem_login_history, viewGroup, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        final NameAndPhone nameAndPhone = mList.get(position);
        if (TextUtils.isEmpty(nameAndPhone.getNickName()))
            viewHolder.mTxtName.setText(nameAndPhone.getPhone());
        else
            viewHolder.mTxtName.setText(nameAndPhone.getNickName());
        String fileName = nameAndPhone.getImageFile();
        File file = new File(fileName);
        if (file.exists()) {
            ImageLoader.loadAvatarImage(mContext, viewHolder.mImgAvatar, 20, file);
        } else {
            ImageLoader.loadAvatarImage(mContext, viewHolder.mImgAvatar, 20, "res://com.sds.smarthome/" + R.mipmap.login_icon);
        }

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewHolder.mTxtName.getLayoutParams();
        params.width = UIUtils.getScreenWidth() / 3;
        viewHolder.mTxtName.setLayoutParams(params);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener != null)
                    mItemClickListener.onClickItem(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mList == null)
            return 0;

        return mList.size();
    }

    public interface ItemClickListener {
        void onClickItem(int pos);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        RoundedImageView mImgAvatar;
        TextView mTxtName;


        public ViewHolder(View itemView) {
            super(itemView);
            mImgAvatar = (RoundedImageView) itemView.findViewById(R.id.img_avatar);
            mTxtName = (TextView) itemView.findViewById(R.id.txt_name);
        }
    }
}
